import axios from 'axios'
import { Loading, Message } from 'element-ui'
export function get(url, callback = result => {}, params = {}) {
  const loading = Loading.service({
    lock: true,
    text: 'loading......',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  axios.get(url,
    { params: params }).then(response => {
    if (response.data.code !== 1) {
      Message({
        message: response.data.message,
        type: 'error'
      })
    }
    callback(response.data)
  }).catch(err => {
    this.$message.error(err)
  }).finally(() => {
    loading.close()
  })
}
export function post(url, params = {}, callback = result => {}) {
  const loading = Loading.service({
    lock: true,
    text: 'waiting......',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  const formData = new FormData()
  for (const key in params) {
    formData.append(key, params[key])
  }
  axios.post(url, formData).then(response => {
    const type = response.data.code === 1 ? 'success' : 'error'
    Message({
      message: response.data.message,
      type: type
    })
    callback(response.data)
  }).catch(err => {
    this.$message.error(err)
  }).finally(() => {
    loading.close()
  })
}
